Tensor Operations

LinearAlgebra.crossMethod
cross(x::Vec{3}, y::Vec{3}) -> Vec{3}
cross(x::Vec{2}, y::Vec{2}) -> Vec{3}
cross(x::Vec{1}, y::Vec{1}) -> Vec{3}
x × y

Compute the cross product between two vectors. The vectors are expanded to 3D frist for dimensions 1 and 2. The infix operator × (written \times) can also be used. x × y (where × can be typed by \times<tab>) is a synonym for cross(x, y).

julia> x = rand(Vec{3})
3-element Tensor{Tuple{3},Float64,1,3}:
 0.5908446386657102
 0.7667970365022592
 0.5662374165061859

julia> y = rand(Vec{3})
3-element Tensor{Tuple{3},Float64,1,3}:
 0.4600853424625171
 0.7940257103317943
 0.8541465903790502

julia> x × y
3-element Tensor{Tuple{3},Float64,1,3}:
  0.20535000738340053
 -0.24415039787171888
  0.11635375677388776
source
LinearAlgebra.dotMethod
dot(x::AbstractTensor, y::AbstractTensor)
x ⋅ y

Compute dot product such as $a = x_i y_i$. This is equivalent to contraction(::AbstractTensor, ::AbstractTensor, Val(1)). x ⋅ y (where can be typed by \cdot<tab>) is a synonym for dot(x, y).

Examples

julia> x = rand(Vec{3})
3-element Tensor{Tuple{3},Float64,1,3}:
 0.5908446386657102
 0.7667970365022592
 0.5662374165061859

julia> y = rand(Vec{3})
3-element Tensor{Tuple{3},Float64,1,3}:
 0.4600853424625171
 0.7940257103317943
 0.8541465903790502

julia> a = x ⋅ y
1.3643452781654772
source
LinearAlgebra.normMethod
norm(::AbstractTensor)

Compute norm of a tensor.

Examples

julia> x = rand(Mat{3, 3})
3×3 Tensor{Tuple{3,3},Float64,2,9}:
 0.590845  0.460085  0.200586
 0.766797  0.794026  0.298614
 0.566237  0.854147  0.246837

julia> norm(x)
1.7377443667834922
source
LinearAlgebra.trMethod
tr(::AbstractSecondOrderTensor)
tr(::AbstractSymmetricSecondOrderTensor)

Compute the trace of a square tensor.

Examples

julia> x = rand(Mat{3,3})
3×3 Tensor{Tuple{3,3},Float64,2,9}:
 0.590845  0.460085  0.200586
 0.766797  0.794026  0.298614
 0.566237  0.854147  0.246837

julia> tr(x)
1.6317075356075135
source
Tensorial.contractionMethod
contraction(::AbstractTensor, ::AbstractTensor, ::Val{N})

Conduct contraction of N inner indices. For example, N=2 contraction for third-order tensors $A_{ij} = B_{ikl} C_{klj}$ can be computed in Tensorial.jl as

julia> B = rand(Tensor{Tuple{3,3,3}});

julia> C = rand(Tensor{Tuple{3,3,3}});

julia> A = contraction(B, C, Val(2))
3×3 Tensor{Tuple{3,3},Float64,2,9}:
 1.36912   1.86751  1.32531
 1.61744   2.34426  1.94101
 0.929252  1.89656  1.79015

Following symbols are also available for specific contractions:

  • x ⊗ y (where can be typed by \otimes<tab>): contraction(x, y, Val(0))
  • x ⋅ y (where can be typed by \cdot<tab>): contraction(x, y, Val(1))
  • x ⊡ y (where can be typed by \boxdot<tab>): contraction(x, y, Val(2))
source
Tensorial.otimesMethod
otimes(x::AbstractTensor, y::AbstractTensor)
x ⊗ y

Compute tensor product such as $A_{ij} = x_i y_j$. x ⊗ y (where can be typed by \otimes<tab>) is a synonym for otimes(x, y).

Examples

julia> x = rand(Vec{3})
3-element Tensor{Tuple{3},Float64,1,3}:
 0.5908446386657102
 0.7667970365022592
 0.5662374165061859

julia> y = rand(Vec{3})
3-element Tensor{Tuple{3},Float64,1,3}:
 0.4600853424625171
 0.7940257103317943
 0.8541465903790502

julia> A = x ⊗ y
3×3 Tensor{Tuple{3,3},Float64,2,9}:
 0.271839  0.469146  0.504668
 0.352792  0.608857  0.654957
 0.260518  0.449607  0.48365
source
Tensorial.rotateMethod
rotate(x::Vec, R::SecondOrderTensor)
rotate(x::SecondOrderTensor, R::SecondOrderTensor)
rotate(x::SymmetricSecondOrderTensor, R::SecondOrderTensor)

Rotate x by rotation matrix R. This function can hold the symmetry of SymmetricSecondOrderTensor.

Examples

julia> A = rand(SymmetricSecondOrderTensor{3})
3×3 Tensor{Tuple{Symmetry{Tuple{3,3}}},Float64,2,6}:
 0.590845  0.766797  0.566237
 0.766797  0.460085  0.794026
 0.566237  0.794026  0.854147

julia> R = rotmatz(π/4)
3×3 Tensor{Tuple{3,3},Float64,2,9}:
 0.707107  -0.707107  0.0
 0.707107   0.707107  0.0
 0.0        0.0       1.0

julia> rotate(A, R)
3×3 Tensor{Tuple{Symmetry{Tuple{3,3}}},Float64,2,6}:
 -0.241332   0.0653796  -0.161071
  0.0653796  1.29226     0.961851
 -0.161071   0.961851    0.854147

julia> R ⋅ A ⋅ R'
3×3 Tensor{Tuple{3,3},Float64,2,9}:
 -0.241332   0.0653796  -0.161071
  0.0653796  1.29226     0.961851
 -0.161071   0.961851    0.854147
source
Tensorial.rotmatMethod
rotmat(θ, n::Vec; degree::Bool = false)

Construct rotation matrix from angle θ and direction n.

julia> x = Vec(1.0, 0.0, 0.0)
3-element Tensor{Tuple{3},Float64,1,3}:
 1.0
 0.0
 0.0

julia> n = Vec(0.0, 0.0, 1.0)
3-element Tensor{Tuple{3},Float64,1,3}:
 0.0
 0.0
 1.0

julia> rotmat(π/2, n) ⋅ x
3-element Tensor{Tuple{3},Float64,1,3}:
 1.1102230246251565e-16
 1.0
 0.0
source
Tensorial.rotmatMethod
rotmat(θ::Real; degree::Bool = false)

Construct 2D rotation matrix.

Examples

julia> rotmat(30, degree = true)
2×2 Tensor{Tuple{2,2},Float64,2,4}:
 0.866025  -0.5
 0.5        0.866025
source
Tensorial.rotmatMethod
rotmat(θ::Vec{3}; sequence::Symbol, degree::Bool = false)
rotmatx(θ::Real)
rotmaty(θ::Real)
rotmatz(θ::Real)

Convert Euler angles to rotation matrix. Use 3 characters belonging to the set (X, Y, Z) for intrinsic rotations, or (x, y, z) for extrinsic rotations.

Examples

julia> α, β, γ = rand(Vec{3});

julia> rotmat(Vec(α,β,γ), sequence = :XYZ) ≈ rotmatx(α) ⋅ rotmaty(β) ⋅ rotmatz(γ)
true

julia> rotmat(Vec(α,β,γ), sequence = :xyz) ≈ rotmatz(γ) ⋅ rotmaty(β) ⋅ rotmatx(α)
true

julia> rotmat(Vec(α,β,γ), sequence = :XYZ) ≈ rotmat(Vec(γ,β,α), sequence = :zyx)
true
source
Tensorial.rotmatMethod
rotmat(a => b)

Construct rotation matrix rotating vector a to b. The norms of two vectors must be the same.

Examples

julia> a = normalize(rand(Vec{3}))
3-element Tensor{Tuple{3},Float64,1,3}:
 0.526847334217759
 0.683741457787621
 0.5049054419691867

julia> b = normalize(rand(Vec{3}))
3-element Tensor{Tuple{3},Float64,1,3}:
 0.36698690362212083
 0.6333543148133657
 0.6813097125956302

julia> R = rotmat(a => b)
3×3 Tensor{Tuple{3,3},Float64,2,9}:
 -0.594528   0.597477   0.538106
  0.597477  -0.119597   0.792917
  0.538106   0.792917  -0.285875

julia> R ⋅ a ≈ b
true
source
Tensorial.skewMethod
skew(::AbstractSecondOrderTensor)
skew(::AbstractSymmetricSecondOrderTensor)

Compute skew-symmetric (anti-symmetric) part of a second order tensor.

source
Tensorial.skewMethod
skew(ω::Vec{3})

Construct a skew-symmetric (anti-symmetric) tensor W from a vector ω as

\[\bm{\omega} = \begin{Bmatrix} \omega_1 \\ \omega_2 \\ \omega_3 \end{Bmatrix}, \quad \bm{W} = \begin{bmatrix} 0 & -\omega_3 & \omega_2 \\ \omega_3 & 0 & -\omega_1 \\ -\omega_2 & \omega_1 & 0 \end{bmatrix}\]

Examples

julia> skew(Vec(1,2,3))
3×3 Tensor{Tuple{3,3},Int64,2,9}:
  0  -3   2
  3   0  -1
 -2   1   0
source